<script setup>
import {onMounted, reactive, ref} from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from "axios";
import { ChatLineSquare, Star, User, Timer } from '@element-plus/icons-vue'

const formInline = reactive({
  courseName: '',
  nickName: '',
  content: '',
  pageNum: 1,
  pageSize: 3
})

const total = ref(0)
const tableData = ref()
const multipleSelection = ref([])
const dialogVisible = ref(false)
const currentMark = ref({})

// 获取课程列表数据
const getCourseList = () => {
  axios.get('/vCoursemark/getCoursemarkByPage', {
    params: formInline
  })
  .then(res => {
    console.log('res', res)
    total.value = res.data.data.total
    tableData.value = res.data.data.list
  })
  .catch(err => {
    console.log('err', err)
  })
}

// 搜索
const onSubmit = () => {
  formInline.pageNum = 1
  getCourseList()
}

// 重置搜索
const onReset = () => {
  formInline.courseName = ''
  formInline.nickName = ''
  formInline.content = ''
  formInline.pageNum = 1
  getCourseList()
}

// 分页
const handleSizeChange = (val) => {
  formInline.pageSize = val
  formInline.pageNum = 1
  getCourseList()
}

const handleCurrentChange = (val) => {
  formInline.pageNum = val
  getCourseList()
}

// 添加多选相关方法
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 删除单条记录
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除这条评价记录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  .then(() => {
    axios.delete('/vCoursemark/deleteByPrimaryKey', {
      params: {
        id: row.id
      }
    })
    .then(res => {
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
      getCourseList() // 刷新列表
    })
    .catch(err => {
      console.log('err', err)
      ElMessage({
        type: 'error',
        message: '删除失败'
      })
    })
  })
  .catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    })
  })
}

// 批量删除方法
const handleBatchDelete = () => {
  if (multipleSelection.value.length === 0) {
    ElMessage({
      type: 'warning',
      message: '请至少选择一条记录'
    })
    return
  }

  ElMessageBox.confirm(
    `确定要删除选中的 ${multipleSelection.value.length} 条记录吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  .then(() => {
    const ids = multipleSelection.value.map(item => item.id)
    const params = new URLSearchParams()
    ids.forEach(id => params.append('ids', id))
    
    axios.delete(`/vCoursemark/deleteCoursemarkBatch?${params.toString()}`)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '批量删除成功'
      })
      getCourseList() // 刷新列表
    })
    .catch(err => {
      console.log('err', err)
      ElMessage({
        type: 'error',
        message: '批量删除失败'
      })
    })
  })
  .catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    })
  })
}

// 查看详情方法
const handleViewDetail = (row) => {
  currentMark.value = row
  dialogVisible.value = true
}

// 页面加载时获取数据
onMounted(() => {
  getCourseList()
})
</script>

<template>
  <div class="course-manager">
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="课程名称">
        <el-input v-model="formInline.courseName" placeholder="请输入课程名称" clearable />
      </el-form-item>
      
      <el-form-item label="评论用户">
        <el-input v-model="formInline.nickName" placeholder="请输入用户昵称" clearable />
      </el-form-item>
      
      <el-form-item label="评价内容">
        <el-input v-model="formInline.content" placeholder="请输入评价内容" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <div class="toolbar" style="margin-bottom: 16px">
      <el-button type="danger" @click="handleBatchDelete">批量删除</el-button>
    </div>

    <!-- 表格 -->
    <el-table 
      :data="tableData" 
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="content" label="评价内容" width="150">
        <template #default="scope">
          <el-text :truncated="true">{{ scope.row.content }}</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="200" />
      <el-table-column prop="courseName" label="课程名称" width="120" />
      <el-table-column prop="nickName" label="评论用户" width="120" />
      <el-table-column prop="grade" label="评分" width="200">
        <template #default="scope">
          <el-rate
            v-model="scope.row.grade"
            disabled
            show-score
            text-color="#ff9900"
            :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
          />
        </template>
      </el-table-column>
      
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="scope">
          <el-button 
            link 
            type="primary" 
            size="small" 
            @click="handleViewDetail(scope.row)"
          >
            查看详情
          </el-button>
          <el-button 
            link 
            type="primary" 
            size="small" 
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="margin-top: 20px"
      v-model:current-page="formInline.pageNum"
      v-model:page-size="formInline.pageSize"
      :page-sizes="[3, 5, 10, 20]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 查看详情弹框 -->
    <el-dialog
      v-model="dialogVisible"
      title="评价详情"
      width="50%"
      :close-on-click-modal="false"
      class="mark-dialog"
    >
      <div class="mark-detail">
        <div class="detail-item">
          <div class="detail-label">
            <el-icon><ChatLineSquare /></el-icon>
            <span>评价内容</span>
          </div>
          <div class="detail-content">{{ currentMark.content }}</div>
        </div>
        <div class="detail-item">
          <div class="detail-label">
            <el-icon><Star /></el-icon>
            <span>评分</span>
          </div>
          <div class="detail-content">
            <el-rate
              v-model="currentMark.grade"
              disabled
              show-score
              text-color="#ff9900"
              :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
            />
          </div>
        </div>
        <div class="detail-info">
          <div class="info-item">
            <div class="detail-label">
              <el-icon><User /></el-icon>
              <span>评论用户</span>
            </div>
            <div class="detail-content">{{ currentMark.nickName }}</div>
          </div>
          <div class="info-item">
            <div class="detail-label">
              <el-icon><Timer /></el-icon>
              <span>评价时间</span>
            </div>
            <div class="detail-content">{{ currentMark.createTime }}</div>
          </div>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false" round>关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.course-manager {
  padding: 20px;
}

.demo-form-inline .el-form-item {
  margin-right: 16px;
}

.el-button + .el-button {
  margin-left: 8px;
}

.toolbar {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.mark-dialog :deep(.el-dialog__header) {
  margin: 0;
  padding: 20px 24px;
  border-bottom: 1px solid #ebeef5;
  background-color: #f8f9fa;
}

.mark-dialog :deep(.el-dialog__title) {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.mark-dialog :deep(.el-dialog__body) {
  padding: 0;
}

.mark-detail {
  padding: 24px;
}

.detail-item {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.detail-label {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  color: #409EFF;
  font-weight: 500;
}

.detail-label .el-icon {
  margin-right: 8px;
  font-size: 18px;
}

.detail-content {
  color: #606266;
  line-height: 1.8;
  font-size: 14px;
  padding: 8px 12px;
  background-color: #f8f9fa;
  border-radius: 4px;
  white-space: pre-wrap;
  word-break: break-all;
}

.detail-info {
  display: flex;
  gap: 24px;
}

.info-item {
  flex: 1;
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.mark-dialog :deep(.el-dialog__footer) {
  padding: 16px 24px;
  border-top: 1px solid #ebeef5;
  background-color: #f8f9fa;
}

.dialog-footer .el-button {
  min-width: 100px;
}
</style>